<template>
  <el-upload action="" :show-file-list="false" :before-upload="beforeUpload">
    <el-button :loading="loading" :disabled="loading" style="margin: 0 15px" type="primary" round>{{ loading ? '导入中' : '导入' }}</el-button>
  </el-upload>
</template>
<script>
import { importFile } from '@/api'

export default {
  props: {
    url: String
  },
  data() {
    return {
      loading: false
    }
  },
  methods: {
    // 上传之前: 获取file对象进行参数验证
    beforeUpload(file) {
      this.loading = true
      importFile(this.url, file).then(res => {
        this.$message.success(res.msg)
        this.$emit('success')
        this.loading = false
      }).catch(() => {
        this.$message.error('导入失败')
        this.loading = false
      })
      return false
    }
  }
}
</script>
<style lang="scss">
</style>
